package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrdersService orderService;
    @Autowired
    private OrderDetailService orderDetailService;


    @PostMapping("submit")
    public R submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单分页查询
     * userPage
     */
    @GetMapping("userPage")
    public R userPage(Integer page, Integer pageSize) {
        //基本的分页
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //所需的分页
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //查询基本的分页数据
        LambdaQueryWrapper<Orders> eq = Wrappers.lambdaQuery(Orders.class)
                .eq(Orders::getUserId, BaseContext.getId())
                .orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage, eq);
        //拷贝基本数据
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<OrdersDto> ordersDtos = new ArrayList<>();

        List<Orders> orders = ordersPage.getRecords();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);

            LambdaQueryWrapper<OrderDetail> eq1 = Wrappers.lambdaQuery(OrderDetail.class)
                    .eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(eq1);

            ordersDto.setOrderDetails(orderDetails);

            ordersDtos.add(ordersDto);
        }

        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }

    /**
     * 订单分页查询
     * userPage
     */
    @GetMapping("page")
    public R Page(Integer page, Integer pageSize, Long number, Date beginTime, Date endTime) {
        //基本的分页
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //数据查询条件
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
                .like(number != null, Orders::getNumber, number)
                .orderByDesc(Orders::getOrderTime);
        //查询基本的分页数据
        orderService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 更改订单状态
     */
    @PutMapping
    public R updateStatus(@RequestBody OrdersDto ordersDto) {
        LambdaUpdateWrapper<Orders> eq = Wrappers.lambdaUpdate(Orders.class)
                .set(ordersDto.getStatus() != null, Orders::getStatus, ordersDto.getStatus())
                .eq(ordersDto.getId() != null, Orders::getId, ordersDto.getId());

        orderService.update(eq);
        return R.success("更改成功");
    }
}
